package com.zdp.剑指offer;

import com.zdp.util.ListNode;

import java.util.Random;

public class ListUtil {
    //输入数组生成链表
    static public ListNode createList(int[] array){
        ListNode head = new ListNode();
        head.next = null;
        ListNode p = head;
        for (int i : array) {
            ListNode node = new ListNode(i);
            node.next = p.next;
            p.next = node;
            p = node;
        }
        return head.next;
    }
    //生成测试数据
    static public ListNode createList(int length){
        ListNode head = new ListNode((int)(Math.random()*length+5));
        ListNode result = head;
        for(int i=0;i<length-1;i++){
            ListNode p = new ListNode((int)(Math.random()*length+5));
            head.next = p;
            head = p;
        }
        return result;
    }
    //生成有序的链表
    //生成测试数据
    static public ListNode createOrderList(int length){
        Random random = new Random();
        int temp = random.nextInt(10);
        //System.out.println("temp = "+temp);
        ListNode head = new ListNode(temp);
        ListNode result = head;
        for(int i=0;i<length-1;i++){
            while(true){
                int temp2 = random.nextInt(i*10+11);
                //System.out.println("temp2 = "+temp2+"===== temp = "+temp);
                if(temp2>=temp){
                    temp = temp2;
                    break;
                }
            }
            ListNode p = new ListNode(temp);
            head.next = p;
            head = p;
        }
        return result;
    }
    //打印链表
    static public void outList(ListNode list){
        while(list!=null){
            if(list.next==null){
                System.out.print(list.val+"\n");
            }else{
                System.out.print(list.val+",");
            }
            list = list.next;
        }
        System.out.println();
    }
}
